<template>
  <lay-card>
    <lay-row space="10">
      <lay-col md="18">
        <lay-select v-model="childNode.name" placeholder="请选择">
          <lay-select-option :value="item" :label="item" v-for="item in db_list"></lay-select-option>
        </lay-select>
      </lay-col>
      <lay-col md="4"><lay-button type="normal" @click="addLinkNode">新增</lay-button></lay-col>
    </lay-row>
  </lay-card>
</template>

<script lang="ts">
export default {
  name: "AddSourceType"
}
</script>

<script lang="ts" setup>
import {onBeforeMount, ref} from "vue";
import {addNodeOrLink, getSourceTypes, updateLink} from "../../api/module/db";
import {notify} from "../../untils/tip";


const {operate, childNode, updateRootNode} = defineProps({
    operate: {type: String},
    childNode: {type: Object},
    updateRootNode: {type: Function}
})

const db_list = ref([]);
const updateDataFunction = ref();

const getAllDBTypes = async () => {
    let {result, status, message} = await getSourceTypes()
    if(status === 'success'){
        db_list.value = result;
    }
};

onBeforeMount(() => {
    console.log('childNode:', childNode)
    updateDataFunction.value = updateRootNode;
    getAllDBTypes();
});


const addLinkNode = async () => {
    if(childNode){
      if(operate === 'add'){
        let {result, status, message} = await addNodeOrLink(childNode)
        if(status === 'success'){
            childNode.id = result.id;
            updateDataFunction.value(childNode)
        }else {
            notify("failed", message)
        }
      }else {
        let {result, status, message} = await updateLink(childNode)
        if(status === 'success'){
            updateDataFunction.value(childNode)
        }else {
            notify("failed", message)
        }
      }
    }
};
</script>

<style scoped>

</style>